package net.office.service.impl;

import net.office.bean.StaffLeave;
import net.office.bean.StaffLeaveExample;
import net.office.bean.StaffTrainExample;
import net.office.mapper.StaffLeaveMapper;
import net.office.service.StaffLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StaffLeaveServiceImpl implements StaffLeaveService {

    @Autowired
    private StaffLeaveMapper staffLeaveMapper;

    @Override
    public List<StaffLeave> findAll() {
        StaffLeaveExample example = new StaffLeaveExample();
        return staffLeaveMapper.selectByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer staffLeaveId) {
        return staffLeaveMapper.deleteByPrimaryKey(staffLeaveId);
    }

    @Override
    public List<StaffLeave> findByStaffLeave(StaffLeave staffLeave) {
        StaffLeaveExample example = new StaffLeaveExample();
        StaffLeaveExample.Criteria criteria = example.createCriteria();

        if(staffLeave.getStaffLeaveId() != null){
            criteria.andStaffLeaveIdEqualTo(staffLeave.getStaffLeaveId());
        }
        if(staffLeave.getStaffId() != null){
            criteria.andStaffIdEqualTo(staffLeave.getStaffId());
        }
        if(staffLeave.getStaffNo() != null
                && staffLeave.getStaffNo().trim().length() != 0){
            criteria.andStaffNoLike("%" + staffLeave.getStaffNo() + "%");
        }
        if(staffLeave.getStaffName() != null
                && staffLeave.getStaffName().trim().length() != 0){
            criteria.andStaffNameLike("%" + staffLeave.getStaffName() + "%");
        }
        if(staffLeave.getStaffLeaveStartDate() != null){
            criteria.andStaffLeaveStartDateEqualTo(staffLeave.getStaffLeaveStartDate());
        }
        if(staffLeave.getStaffLeaveEndDate() != null){
            criteria.andStaffLeaveEndDateEqualTo(staffLeave.getStaffLeaveEndDate());
        }
        if(staffLeave.getStaffLeaveReason() != null
                && staffLeave.getStaffLeaveReason().trim().length() != 0){
            criteria.andStaffLeaveReasonLike("%" + staffLeave.getStaffLeaveReason() + "%");
        }
        if(staffLeave.getStaffLeaveSubmitTime() != null){
            criteria.andStaffLeaveSubmitTimeEqualTo(staffLeave.getStaffLeaveSubmitTime());
        }
        if(staffLeave.getStaffLeaveApproveResult() != null){
            criteria.andStaffLeaveApproveResultEqualTo(staffLeave.getStaffLeaveApproveResult());
        }
        if(staffLeave.getStaffLeaveApproveStaffId() != null){
            criteria.andStaffLeaveApproveStaffIdEqualTo(staffLeave.getStaffLeaveApproveStaffId());
        }
        if(staffLeave.getStaffLeaveApproveStaffNo() != null
                && staffLeave.getStaffLeaveApproveStaffNo().trim().length() != 0){
            criteria.andStaffLeaveApproveStaffNoLike("%" + staffLeave.getStaffLeaveApproveStaffNo() + "%");
        }
        if(staffLeave.getStaffLeaveApproveStaffName() != null
                && staffLeave.getStaffLeaveApproveStaffName().trim().length() != 0){
            criteria.andStaffLeaveApproveStaffNameLike("%" + staffLeave.getStaffLeaveApproveStaffName() + "%");
        }
        if(staffLeave.getStaffLeaveApproveTime() != null){
            criteria.andStaffLeaveApproveTimeEqualTo(staffLeave.getStaffLeaveApproveTime());
        }

        example.or(criteria);
        return staffLeaveMapper.selectByExample(example);
    }

    @Override
    public int insert(StaffLeave staffLeave) {
        return staffLeaveMapper.insertSelective(staffLeave);
    }

    @Override
    public StaffLeave findOneByStaffLeaveId(Integer staffLeaveId) {
        return staffLeaveMapper.selectByPrimaryKey(staffLeaveId);
    }

    @Override
    public int updateByPrimaryKey(StaffLeave staffLeave) {
        return staffLeaveMapper.updateByPrimaryKey(staffLeave);
    }

    @Override
    public int updateByPrimaryKeySelective(StaffLeave staffLeave) {
        return staffLeaveMapper.updateByPrimaryKeySelective(staffLeave);
    }

    @Override
    public List<StaffLeave> findAllByStaffId(int staffId) {
        StaffLeaveExample example = new StaffLeaveExample();
        example.createCriteria().andStaffIdEqualTo(staffId);

        return staffLeaveMapper.selectByExample(example);
    }
}
